package com.atguigu.educms.controller;


import cn.hutool.core.util.ObjectUtil;
import com.atguigu.commonutils.R;
import com.atguigu.educms.entity.CrmBanner;
import com.atguigu.educms.service.CrmBannerService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 首页banner表 前端控制器
 * </p>
 *
 * @author ZhanK
 * @since 2022-05-10
 */
@RestController
@RequestMapping("/educms/banneradmin")
@CrossOrigin
@Api(value = "轮播图数据的增删改查")
public class BannerAdminController {

    @Autowired
    CrmBannerService crmBannerService;

    @ApiOperation(value = "条件查询分页方法")
    @PostMapping(value = "pageBanner/{current}/{limit}")
    public R pageBanner(@PathVariable long current, @PathVariable long limit,
                        @RequestBody(required = false) CrmBanner crmBanner) {
        //分页查询轮播图
        return crmBannerService.getPageBanners(current, limit, crmBanner);

    }

    @ApiOperation(value = "增加轮播图")
    @PostMapping("addBanner")
    public R addBanner(@RequestBody CrmBanner crmBanner) {

        boolean save = crmBannerService.save(crmBanner);
        if (save) {
            return R.ok();
        } else
            return R.error();

    }

    @ApiOperation(value = "删除方法")
    @DeleteMapping("{id}")
    public R deleteBannerById(@PathVariable String id) {
        if (crmBannerService.removeById(id)) {
            return R.ok();
        } else return R.error();

    }
    @ApiOperation(value = "修改方法")
    @PutMapping("updateBammer")
    public R updateBammer(@RequestBody CrmBanner crmBanner){
        if (crmBannerService.updateById(crmBanner)){
            return R.ok();
        }
        else return R.error();
    }

    @ApiOperation(value = "根据id查询方法")
    @GetMapping("{id}")
    public R getById (@PathVariable("id") String id ){

        CrmBanner crmBanner= crmBannerService.getById(id);
        return R.ok().data("crmBanner",crmBanner);
    }

}

